/**
 * ATuanZhang
 */
package com.atuanzhang.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.huanglei.android.inject.annotation.InjectService;
import org.huanglei.android.util.AndroidHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.atuanzhang.Constant;
import com.atuanzhang.bean.Goods;
import com.atuanzhang.cache.Cache;
import com.atuanzhang.service.GoodsService;
import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * @author huanglei
 * @date 2011-3-25 @
 */
@InjectService
public class GoodsServiceImpl implements GoodsService {

	private final static String TAG = "GoodsService";

	private final static String KEY_GOODS = "goods";
	
	private final static String KEY_CITY = "city";
	
	private final static String KEY_COUNT = "count";
	
	public static String currentCity = "";
	
	public static int  count = 0;

	@Inject
	private Cache cache = null;

	@Inject
	protected Provider<AndroidHttpClient> httpClientProvider = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.atuanzhang.service.GoodsService#findGoodsByCity(java.lang.String)
	 */
	@Override
	public List<Goods> findGoodsByCity(String city) {
		try {
			
			HttpResponse response = httpClientProvider.get().httpGet(
					Constant.REMOTE_URL_GOODS + "?city="
							+ URLEncoder.encode(city, HTTP.UTF_8));
			
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				
				try {
					
					JSONObject jsonObject = new JSONObject(
							EntityUtils.toString(response.getEntity(),
									HTTP.UTF_8));
					
					currentCity = jsonObject.getString(KEY_CITY);
					
					count = jsonObject.getInt(KEY_COUNT);
					
					JSONArray goodsArrayJson = jsonObject
							.getJSONArray(KEY_GOODS);

					List<Goods> goodsList = new ArrayList<Goods>();

					Goods goods = null;

					for (int i = 0; i < goodsArrayJson.length(); ++i) {
						JSONObject goodsJson = goodsArrayJson.getJSONObject(i);
						goods = new Goods();
						goods.setId(goodsJson.getLong("id"));
						goods.setTitle(goodsJson.getString("title"));
						goods.setWebsite(goodsJson.getString("website"));
						goods.setLoc(goodsJson.getString("loc"));
						goods.setCity(goodsJson.getString("city"));
						goods.setCityPinyin(goodsJson.getString("city_pinyin"));
						goods.setImage(goodsJson.getString("image"));
						goods.setValue(goodsJson.getDouble("value"));
						goods.setPrice(goodsJson.getDouble("price"));
						goodsList.add(goods);
						System.out.println(goods);
					}
					return goodsList;
				} catch (JSONException e) {
					Log.e(TAG, "findGoodsByCity JSONException", e);
				}
			}
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, "findGoodsByCity UnsupportedEncodingException", e);
		} catch (IOException e) {
			Log.e(TAG, "findGoodsByCity IOException", e);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.atuanzhang.service.GoodsService#findGoodsByGoodsId(long)
	 */
	@Override
	public Goods findGoodsInfoByGoodsId(long goodsId) {
		return (Goods) cache.get("" + goodsId);
	}

}
